这个问题可能是重复的这一个.但答案对我不起作用,我希望更具体.
我有一个NSString
,但我需要一个NS(Mutable)AttributedString
和这个字符串中的一些单词应该给出不同的颜色.我试过这个:
NSString *text = @"This is the text and i want to replace something";
NSDictionary *attributes = @ {NSForegroundColorAttributeName : [UIColor redColor]};
NSMutableAttributedString *subString = [[NSMutableAttributedString alloc] initWithString:@"AND" attributes:attributes];
NSMutableAttributedString *newText = [[NSMutableAttributedString alloc] initWithString:text];
newText = [[newText mutableString] stringByReplacingOccurrencesOfString:@"and" withString:[subString mutableString]];
Run Code Online (Sandbox Code Playgroud)
"和"应该是大写的红色.
文档说mutableString保留属性映射.但是对于我的替换事物,我在赋值的右侧(在我的代码片段的最后一行)中没有更多的claimString.
我怎样才能得到我想要的东西?;)
我想在一些UITextViews中调整字体大小.这适用于插座集合和此代码:
for (UITextView *view in self.viewsToResize) {
if ([view respondsToSelector:@selector(setFont:)]) {
[view setFont:[UIFont systemFontOfSize:view.font.pointSize + 5]];
}
}
Run Code Online (Sandbox Code Playgroud)
但我的问题是,并非每个textView都使用systemFont正常的重量,其中一些是粗体重.是否有可能获得字体重量?随着po view.font
在调试方面,我可以看到我需要的一切:
$11 = 0x0c596ea0 <UICFFont: 0xc596ea0> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 12px
Run Code Online (Sandbox Code Playgroud)
但是我如何访问字体权重?
使用第二个插座集合来获取大胆的视图可以解决我的问题.但我想知道我没有发现只有字体重量.