UITextView underlined text

笑着哭i 提交于 2019-12-08 06:11:38

问题


I have UITextView and want to underline selected text. It is working by this code

NSRange range = selectedTextView.selectedRange;
NSTextStorage *textStorage = selectedTextView.textStorage;
[textStorage addAttribute: NSUnderlineStyleAttributeName
                  value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
                  range:range];

But letters which have "a tail" under baseline (as q y g p) are not underlined (screenshot: http://i.stack.imgur.com/dRrEH.png). Also I noticed that space between baseline and underline depends on font.

How can I underline text without any breakings/spacing?


回答1:


#import <UIKit/UIKit.h>

@interface TextViewWithUnderline : UITextView

@end

#import "TextViewWithUnderline.h"

@implementation TextViewWithUnderline

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        //self.contentMode = UIViewContentModeRedraw;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

    //Get the current drawing context
    CGContextRef context = UIGraphicsGetCurrentContext();
    //Set the line color and width
    CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor);
    CGContextSetLineWidth(context, 1.0f);
    //Start a new Path
    CGContextBeginPath(context);

    //Find the number of lines in our textView + add a bit more height to draw lines in the empty part of the view
    NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.leading;

    //Set the line offset from the baseline. (I'm sure there's a concrete way to calculate this.)
    CGFloat baselineOffset = 6.0f;

    //iterate over numberOfLines and draw each line
    for (int x = 1; x < numberOfLines; x++) {
        //0.5f offset lines up line with pixel boundary
        CGContextMoveToPoint(context, self.bounds.origin.x + 10, self.font.leading*x + 0.5f + baselineOffset);
        CGContextAddLineToPoint(context, self.bounds.size.width - 10, self.font.leading*x + 0.5f + baselineOffset);
    }

    //Close our Path and Stroke (draw) it
    CGContextClosePath(context);
    CGContextStrokePath(context);
}

@end

#import <UIKit/UIKit.h>
#import "TextViewWithUnderline.h"
@interface CustomTextView : UIViewController
@property (weak, nonatomic) IBOutlet TextViewWithUnderline *textView;

@end

#import "CustomTextView.h"

@interface CustomTextView ()

@end

@implementation CustomTextView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.textView.contentMode = UIViewContentModeRedraw;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


来源:https://stackoverflow.com/questions/21927810/uitextview-underlined-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!