2

嘿,我正在制作一个需要在文本视图的文本的某个部分加下划线的应用程序。有没有一种简单的方法可以让它变成粗体和斜体,或者我必须制作和导入自定义字体?我在这里先向您的帮助表示感谢!

4

3 回答 3

2

这就是我所做的。它像黄油一样起作用。

1)添加CoreText.framework到您的框架。

2)在需要下划线标签的类中导入<CoreText/CoreText.h>

3) 编写以下代码。

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"My Messages"];
[attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName
                  value:[NSNumber numberWithInt:kCTUnderlineStyleSingle]
                  range:(NSRange){0,[attString length]}];
self.myMsgLBL.attributedText = attString;
self.myMsgLBL.textColor = [UIColor whiteColor];
于 2013-10-28T07:42:12.173 回答
1
#import <UIKit/UIKit.h>

@interface TextFieldWithUnderLine : UITextField

@end

#import "TextFieldWithUnderLine.h"

@implementation TextFieldWithUnderLine

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

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

    // offset lines up - we are adding offset to font.leading so that line is drawn right below the characters and still characters are visible.
    CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading + 4.0f);
    CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading + 4.0f);

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

@end
于 2015-06-22T06:57:21.643 回答
0

自 iOS 6.0 起,UILabel支持使用属性文本属性显示属性字符串UITextFieldUITextView

用法:

NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc] initWithString:@"text"];
[aStr addAttribute:NSUnderlineStyleAttributeName value:NSUnderlineStyleSingle range:NSMakeRange(0,2)];
label.attributedText = aStr;
于 2012-09-14T11:42:16.787 回答