2014-02-28 50 views
-2

我想,我的TextView文本類似於此:試圖讓TextView的初始文本ios7

PARA1:

PARA2:

,這樣的文字會以粗體文字樣式和特定的字體,並在運行時,我希望其他文字將以不同的風格。

回答

1

是的,你需要使用NSAttributedString的是,發現這有助於改變字的顏色RunningAppHere,同樣可以改變字體

點是使用NSAttributedString

通覽單詞,並找到你的單詞的範圍並改變它的顏色。

- (IBAction)colorWord:(id)sender { 
NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.text.text]; 

NSArray *words=[self.text.text componentsSeparatedByString:@" "]; 

for (NSString *word in words) {   
    if ([word hasPrefix:@"@"]) { 
     NSRange range=[self.text.text rangeOfString:word]; 
     [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];   
    } 
} 
[self.text setAttributedText:string]; 
}