2011-08-16 73 views
1

是否有簡單的方式在iOS 4中強調文本?在IB可能嗎?在此先感謝...iPhone:下劃線文字

+0

更具體的你想要做什麼。 –

+0

我絕對不同意你的看法。問題很清楚... – Jim

+0

[下劃線文本在UIlabel]中可能的重複(http://stackoverflow.com/questions/2711297/underline-text-in-uilabel) –

回答

4

據我所知,你不能強調文字。

您可以通過(例如)將UImageView或常規View放置在下方,並使用與文本相同的顏色來「僞造」它。你可以使用穿透等,但不能強調。

編輯: 雖然你可以用這種方法來強調你的UILabel。你可以使用自定義的UILabel。所以,你可以創建一些類像CUILabel繼承UILabel,並取代其drawRect方法在@implementation部分如下:

- (void)drawRect:(CGRect)rect { 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(ctx, 0.0f/255.0f, 0.0f/255.0f, 255.0f/255.0f, 1.0f); // Your underline color 
    CGContextSetLineWidth(ctx, 1.0f); 

    UIFont *font = [UIFont systemFontOfSize:16.0f]; 
    CGSize constraintSize = CGSizeMake(MAXFLOAT, MAXFLOAT); 
    CGSize labelSize; 
    labelSize = [self.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    CGContextMoveToPoint(ctx, 0, self.bounds.size.height - 1); 
    CGContextAddLineToPoint(ctx, labelSize.width + 10, self.bounds.size.height - 1); 

    CGContextStrokePath(ctx); 

    [super drawRect:rect]; 
} 
+2

他可以強調。通過子類uilabel –

3

只要繼承的UILabel和drawRect中繪製你的文字只是繪製文本下一個簡單的行之後。看看StrikeUILabel它有一些錯誤,但你可以從那個類開始。

0

NSMutableAttributedString/NSAttributedString允許您創建具有各種豐富屬性(如字體,顏色,字體樣式(包括下劃線))的文本。對於一般信息請點擊此鏈接..

Introduction to Attributed String Programming Guide

具體強調文本,該鏈接可以幫助.. Changing an Attributed String

+0

與Mac OS X不同,在iOS中不能繪製屬性字符串。 – titaniumdecoy

+0

IOS 6現在已將字符串歸因於: http://stackoverflow.com/questions/15328704/how-can-i-underline-text-in-ios-6 – Refactor

0

有時候我更喜歡使用的UILabel作爲下劃線與下劃線顏色,背景不文本,高度1或2像素和寬度等於文本要加下劃線...