How do I add tab stops to an NSAttributedString and display in a UITextView

后端 未结 4 1889
一整个雨季
一整个雨季 2020-12-30 13:00

I\'m creating an iOS app, and I would like to display an attributed string with specific tab stops specified in a UITextView. I would also like to draw them directly into U

4条回答
  •  长发绾君心
    2020-12-30 13:12

    In iOS 7 you can do it like this:

    UIFont *font = [UIFont systemFontOfSize:18.0];
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    NSInteger cnt;
    CGFloat tabInterval = 72.0;
    paragraphStyle.defaultTabInterval = tabInterval;
    NSMutableArray *tabs = [NSMutableArray array];
    for (cnt = 1; cnt < 13; cnt++) {    // Add 12 tab stops, at desired intervals...
        [tabs addObject:[[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft location:tabInterval * cnt options:nil]];
    }
    paragraphStyle.tabStops = tabs;
    NSDictionary *attributes = @{ NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle};
    

提交回复
热议问题