How can I set the color and alignment of attributed text in a UITextView in iOS 7?

前端 未结 1 1091
名媛妹妹
名媛妹妹 2020-12-29 02:27

The formatting of my textViews worked fine in iOS 6, but no longer in iOS 7. I understand with Text Kit much of the under the hood stuff has changed. It\'s become really qui

相关标签:
1条回答
  • 2020-12-29 03:18

    Curious, the properties are taken into account for UILabel but not for UITextView

    Why don't you just add attributes for color and alignment to the attributed string similar to the way you are doing with the font?

    Something like:

    NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"];
    UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60];
    [title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];
    
    //add color
    [title addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, title.length)];
    
    //add alignment
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setAlignment:NSTextAlignmentCenter];
    [title addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, title.length)];
    
    titleView.attributedText = title;
    

    Edit: Assign the text first, then change the properties and this way it works.

    UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];
    
    //create attributed string and change font
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"];
    UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60];
    [title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];
    
    //assign text first, then customize properties
    titleView.attributedText = title;
    titleView.textAlignment = NSTextAlignmentCenter;
    titleView.textColor = [UIColor whiteColor];
    
    0 讨论(0)
提交回复
热议问题