gpt4 book ai didi

ios - 如何绘制NSString atPoint withAttributes 并调整SIZE?

转载 作者:行者123 更新时间:2023-11-29 03:17:48 25 4
gpt4 key购买 nike

我能够绘制我的 NSString,但在绘制它们时我无法调整大小。我的 drawRect: 方法是:

- (void)drawRect:(CGRect)rect
{

NSArray *objectArray = [NSArray arrayWithObjects:[UIFont systemFontOfSize:80.0f], nil];
NSArray *keyArray = [NSArray arrayWithObjects:@"NSFontAttributeName", nil];
NSMutableDictionary *textAttributes = [NSMutableDictionary dictionaryWithObjects:objectArray forKeys:keyArray];
NSString *myTestString = @"Test String";
[textAttributes setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
[myTestString drawAtPoint:CGPointMake(20, 30) withAttributes:textAttributes];
[myTestString drawInRect:CGRectMake(50, 50, 500, 500) withAttributes:textAttributes];

NSLog(@"wrote %@ with %@", myTestString, textAttributes);
}

textAttributes看起来不错,返回的字体信息为:

 NSFontAttributeName = "<UICTFont: 0x14eeff40> font-family: \".HelveticaNeueInterface-M3\"; font-weight: normal; font-style: normal; font-size: 80.00pt"

我可以使用属性数组正确更改颜色,为什么这会导致文本默认为 10pt 大小?

最佳答案

NSAttributedString 就是你想要的。 UICatalog 示例代码中有一个 NSAttributedString 用法示例:

#pragma mark - UIPickerViewDataSource

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSMutableAttributedString *attrTitle = nil;

// note: for the custom picker we use custom views instead of titles
if (pickerView == self.myPickerView)
{
if (row == 0)
{
NSString *title;
if (component == 0)
title = [self.pickerViewArray objectAtIndex:row];
else
title = [[NSNumber numberWithInt:row] stringValue];

// apply red text for normal state
attrTitle = [[NSMutableAttributedString alloc] initWithString:title];
[attrTitle addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, [attrTitle length])];
}
}

return attrTitle;
}

iOS 上的斯坦福大学 MOOC 类(class)(由 Paul Hegarty 和 available on iTunes 运营)在第 4 讲中概述了 NSAttributedString 的使用。第 5 讲还提供了一个 NSAttributedString 代码演示,您可以按照该演示进行操作。最后github用户m2mtech发表了repositories of all code exercises and assignments for the course ,可以下载相关工程文件here .

关于ios - 如何绘制NSString atPoint withAttributes 并调整SIZE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21469384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com