嘿,我正在制作一个应用程序,要求将文本视图文本的某个部分加下划线。有没有一种简单的方法来做到这一点,如使它粗体和斜体,或者我必须制作和导入自定义字体?我在这里先向您的帮助表示感谢!在xcode中使文字下划线
2
A
回答
0
由于iOS 6.0 UILabel
,UITextField
和UITextView
支持使用属性文本属性显示attributed strings。
用法:
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc] initWithString:@"text"];
[aStr addAttribute:NSUnderlineStyleAttributeName value:NSUnderlineStyleSingle range:NSMakeRange(0,2)];
label.attributedText = aStr;
3
这就是我所做的。它的作用像黄油。
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];
0
#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
相关问题
- 1. 下划线文字CSS峡
- 2. 加下划线的文字
- 3. 的Cufón:下划线文字
- 4. 带下划线的文字
- 5. iPhone:下划线文字
- 6. JToggleButton带下划线文字
- 7. 虚线下划线文本
- 8. Xcode关闭下划线(编辑器)
- 9. 在Ruby on Rails中使用短划线`-`而不是下划线`_`
- 10. 在文件名中带有下划线
- 11. 只在文本中加下划线的最后一行文字
- 12. 下划线QLabel的文字“悬停”
- 13. 文字背后的粗体下划线
- 14. 如何用UIWebView画下划线文字
- 15. 下划线涵盖文字NSAttributedString
- 16. 在datagridview中加下划线
- 17. 忽略下划线字母
- 18. 在CSS中使用斜角下划线
- 19. 在主机名中使用下划线
- 20. 下划线使用iText的字符串
- 21. 下划线在Python
- 22. 正则表达式字母,数字,短划线和下划线
- 23. matplotlib:在文本框中使用LaTeX时,在字符串中覆盖下划线
- 24. 下划线编辑文本
- 25. 下划线的UILabel文本
- 26. 全文为下划线
- 27. uitextview内下划线文本
- 28. 在<input>框中加下划线文字
- 29. iPhone:如何在按钮中显示带下划线的文字?
- 30. 如何在Android Studio中为文字加下划线?
感谢的人生病让这个尝试 –
东西是不工作;这里是我的代码,你看到任何突出的错误? NSMutableAttributedString * aStr = [[NSMutableAttributedString alloc] initWithString:BookTitle.text]; [aStr addAttribute:NSUnderlineStyleAttributeName value:NSUnderlineStyleSingle range:NSMakeRange(0,2)]; BookTitle.attributedText = aStr; –
其中BookTitle是一个textFeild –