UILabel字體加粗等屬性和特效

/*   php

 Accessing the Text Attributes   html

 text  property     web

 font  property     框架

 textColor  property     ide

 textAlignment  property     函數

 lineBreakMode  property       字體

 enabled  property     url

 Sizing the Label’s Text   spa

 adjustsFontSizeToFitWidth  property     code

 baselineAdjustment  property     

 minimumFontSize  property   無例   

 numberOfLines  property     

 Managing Highlight Values   

 highlightedTextColor  property     

 highlighted  property     

 Drawing a Shadow   

 shadowColor  property     

 shadowOffset  property     

 Drawing and Positioning Overrides   

 – textRectForBounds:limitedToNumberOfLines: 無例    

 – drawTextInRect:  無例   

 Setting and Getting Attributes   

 userInteractionEnabled  property     

 */ 

 

 

UILabel UIFont // default system font 17 plain

 

 


UILabel垂直居上對齊[label sizeToFit];

//設置文字過長時的顯示格式 

label.lineBreakMode = UILineBreakModeWordWrap;

 

typedefenum {

    UILineBreakModeWordWrap =0,           // Wrap at word boundaries

    UILineBreakModeCharacterWrap,          // Wrap at character boundaries

    UILineBreakModeClip,           //截去多餘部分 Simply clip when it hits the end of the rect截去多餘部分 

    UILineBreakModeHeadTruncation, //截去頭部Truncate at head of line: "...wxyz". Will truncate multiline text on first line

    UILineBreakModeTailTruncation,//截去尾部 Truncate at tail of line: "abcd...". Will truncate multiline text on last line

    UILineBreakModeMiddleTruncation,//截去中間 Truncate middle of line:  "ab...yz". Will truncate multiline text in the middle

} UILineBreakMode;

 

//設置label的行數,這個能夠根據上節的UITextView自適應高度 

label.numberOfLines = 2;

label.lineBreakMode = UILineBreakModeWordWrap;

label.textAlignment =  UITextAlignmentCenter;//設置文字對齊位置,居左,居中,居右 

label.text = @ "123" ;//設置顯示文字 

//設置文字顏色,能夠有多種顏色能夠選擇

label.textColor = [UIColor whiteColor];

label.backgroundColor = [UIColor blackColor];

//設置字體:粗體,正常的是 SystemFontOfSize,調用系統的字體配置 

label.font = [UIFont boldSystemFontOfSize:20];

label.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(36.0)];

 

//[UIFont fontWithName:@ "Arial" size:14.0]]; //非加粗

 

//設置文本是否高亮和高亮時的顏色

scoreLabel.highlighted = YES; 

scoreLabel.highlightedTextColor = [UIColor orangeColor]; 

//設置陰影的顏色和陰影的偏移位置 

scoreLabel.shadowColor = [UIColor redColor]; 

scoreLabel.shadowOffset = CGSizeMake(1.0,1.0); 

//設置是否能與用戶進行交互 

scoreLabel.userInteractionEnabled = YES;  

//設置label中的文字是否可變,默認值是YES  

scoreLabel.enabled = NO;

 

//設置字體大小是否適應label寬度 

label.adjustsFontSizeToFitWidth = YES; 

//若是adjustsFontSizeToFitWidth屬性設置爲YES,這個屬性就來控制文本基線的行爲

 

coreLabel.baselineAdjustment = UIBaselineAdjustmentNone

 

typedefenum {

    UIBaselineAdjustmentAlignBaselines =0,// default. used when shrinking text to position based on the original baseline

    UIBaselineAdjustmentAlignCenters,

    UIBaselineAdjustmentNone,

} UIBaselineAdjustment;

 

//最小文字號數

minimumFontSize

設置背景色爲透明 

 

 

scoreLabel.backgroudColor=[UIColor clearColor];

自定義的顏色:

scoreLabel.backgroudColor=[UIColor clearColor];

 

UIColor *color = [UIColor colorWithRed:1.0f green:50.0f blue:0.0f alpha:1.0f];

scoreLabel.textColor = [UIColor color]

//UIColor 裏的 RGB 值是CGFloat類型的在0~1範圍內,對應0~255的顏色值範圍。

 

 

 

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;

//改變繪文字屬性.重寫時調用super能夠按默認圖形屬性繪製,若本身徹底重寫繪製函數,就不用調用super了.

- (void)drawTextInRect:(CGRect)rect;

 

eg:

 

UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(0, 0, 75, 40)];   //聲明UIlbel並指定其位置和長寬
 label.backgroundColor = [UIColorclearColor];   //設置label的背景色,這裏設置爲透明色。
 label.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];   //設置label的字體和字體大小。
//lable的旋轉
 label.transform = CGAffineTransformMakeRotation(0.1);     //設置label的旋轉角度
 label.text = @「helloworld」;   //設置label所顯示的文本
 label.textColor = [UIColorwhiteColor];    //設置文本的顏色
 label.shadowColor = [UIColorcolorWithWhite:0.1falpha:0.8f];    //設置文本的陰影色彩和透明度。
 label.shadowOffset = CGSizeMake(2.0f, 2.0f);     //設置陰影的傾斜角度。
 label.textAlignment = UITextAlignmentCenter;     //設置文本在label中顯示的位置,這裏爲居中。
//換行技巧:以下換行可實現多行顯示,但要求label有足夠的寬度。
 label.lineBreakMode = UILineBreakModeWordWrap;     //指定換行模式
 label.numberOfLines = 2;    // 指定label的行數

 

 

 

 

 

 

讓label自適應裏面的文字,自動調整寬度和高度的

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];這個frame是初設的,不要緊,後面還會從新設置其size。
[label setNumberOfLines:0];
NSString *s = @"string......";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];
這樣就能夠對s賦值讓其自動調整其大小了。

 

UILabel跑馬燈效果

 

//http://www.cocoachina.com/bbs/read.php?tid=74540

 

#import <UIKit/UIKit.h>

 

 

@interface TextFlowView : UIView {

    

    //顯示文本的標籤

    UILabel *_firstLabel;

    UILabel *_secondLabel;

    

    //定時器

    NSTimer *_timer;

    

    //顯示的文本

    NSString *_text;

    

    //是否須要滾動

    BOOL _needFlow;

    

    //控件的框架大小

    CGRect _frame;

    

    //文本的字體

    UIFont *_font;

    

    //當前第一個控件的索引

    NSInteger _startIndex;

    

    //定時器每次執行偏移後,累計的偏移量之和

    CGFloat _XOffset;

    

    //文本顯示一行,須要的框架大小

    CGSize _textSize;

    

}

 

- (id)initWithFrame:(CGRect)frame Text:(NSString *)text;

- (void)setFont:(UIFont *)font;

- (void)setText:(NSString *)text;

@end

 

 

//////////////////////////////////////////////////////////////////////////////////

 

 

#import "TextFlowView.h"

 

 

@implementation TextFlowView

 

#pragma mark -

#pragma mark 內部調用

 

#define SPACE_WIDTH 50

#define LABEL_NUM 2

 

//改變一個TRect的起始點位置,可是其終止店點的位置不變,所以會致使整個框架大小的變化

- (CGRect)moveNewPoint:(CGPoint)point rect:(CGRect)rect

{

    CGSize tmpSize;

    tmpSize.height = rect.size.height + (rect.origin.y - point.y);

    tmpSize.width = rect.size.width + (rect.origin.x - point.x);

    returnCGRectMake(point.x, point.y, tmpSize.width, tmpSize.height);

}

//開啓定時器

- (void)startRun

{

    _timer = [NSTimerscheduledTimerWithTimeInterval:0.02target:selfselector:@selector(timerAction) userInfo:nilrepeats:YES];

}

 

//關閉定時器

- (void)cancelRun

{

    if (_timer) 

    {

        [_timerinvalidate];

    }

}

 

//定時器執行的操做

- (void)timerAction

{

    staticCGFloat offsetOnce = -1;

    _XOffset += offsetOnce;

    if (_XOffset +  _textSize.width <= 0) 

    {

        _XOffset += _textSize.width;

        _XOffset += SPACE_WIDTH;

    }

    [selfsetNeedsDisplay];

    

}

 

//計算在給定字體下,文本僅顯示一行須要的框架大小

- (CGSize)computeTextSize:(NSString *)text

{

    if (text == nil) 

    {

        returnCGSizeMake(0, 0);

    }

    CGSize boundSize = CGSizeMake(10000, 100);

    CGSize stringSize = [_textsizeWithFont:_fontconstrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap];

    return stringSize;

}

 

 

- (id)initWithFrame:(CGRect)frame Text:(NSString *)text

{

    self = [superinitWithFrame:frame];

    if (self) 

    {

        _text = [text retain];

        _frame = frame;

        //默認的字體大小

        _font = [UIFontsystemFontOfSize:16.0F];

        self.backgroundColor = [UIColorredColor];

        //初始化標籤

        //判斷是否須要滾動效果

        _textSize = [selfcomputeTextSize:text];

        //須要滾動效果

        if (_textSize.width > frame.size.width) 

        {

            _needFlow = YES;

            [selfstartRun];

        }

    }

    returnself;

}

 

 

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    CGContextRef context= UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [UIColorwhiteColor].CGColor);

    // Drawing code

    CGFloat startYOffset = (rect.size.height - _textSize.height)/2;

    CGPoint origin = rect.origin;

    if (_needFlow == YES) 

    {

//        NSLog(@"OFFSETX:%f", _XOffset);

//        NSLog(@"textwidth:%f",_textSize.width);

        rect = [selfmoveNewPoint:CGPointMake(_XOffset, startYOffset) rect:rect];

//        NSLog(@"rect X:%f  Y:%f",rect.origin.x, rect.origin.y);

//        NSLog(@"rect W:%f  H:%f", rect.size.width, rect.size.height);

        while (rect.origin.x <= rect.size.width+rect.origin.x) 

        {

            [_textdrawInRect:rect withFont:_font];

            rect = [selfmoveNewPoint:CGPointMake(rect.origin.x+_textSize.width+SPACE_WIDTH, rect.origin.y) rect:rect];

//            NSLog(@"inner->rect X:%f  Y:%f",rect.origin.x, rect.origin.y);

//            NSLog(@"inner->rect W:%f  H:%f", rect.size.width, rect.size.height);

        }

        

    }

    else

    {

        //在控件的中間繪製文本

        origin.x = (rect.size.width - _textSize.width)/2;

        origin.y = (rect.size.height - _textSize.height)/2;

        rect.origin = origin;

        [_textdrawInRect:rect withFont:_font];

    }

}

 

 

- (void)dealloc

{

    [_textrelease];

    [superdealloc];

}

 

 

#pragma mark -

#pragma mark 外部調用

- (void)setFont:(UIFont *)font

{

    _font = font;

}

 

- (void)setText:(NSString *)text

{

    [_textrelease];

    _text = [text retain];

 

}

@end

 

 



美化UILabel中的字體代碼分享 
http://www.devdiv.com/iOS_iPhone-%E7%BE%8E%E5%8C%96UILabel%E4%B8%AD%E7%9A%84%E5%AD%97%E4%BD%93%E4%BB%A3%E7%A0%81%E5%88%86%E4%BA%AB-thread-122319-1-1.html
UILabel跑馬燈效果
http://hi.baidu.com/suxinde2009/blog/item/5bcd0e60dd9bb77f0d33fac3.html
分享一個可垂直頂端對齊的UILabel
http://www.devdiv.com/%E5%88%86%E4%BA%AB%E4%B8%80%E4%B8%AA%E5%8F%AF%E5%9E%82%E7%9B%B4%E9%A1%B6%E7%AB%AF%E5%AF%B9%E9%BD%90%E7%9A%84UILabel-weblog-64796-7239.html
 
//儂儂官網鏈接  
    UILabel *labelGovUrl = [[UILabel alloc] initWithFrame:CGRectMake(73.0, 330.0, 180.0, 40.0)];  
    labelGovUrl.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);  
    labelGovUrl.text = @"儂儂官網 >";  
    labelGovUrl.backgroundColor = [UIColor clearColor];  
    labelGovUrl.textColor = [UIColor whiteColor];  
    labelGovUrl.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];  
    labelGovUrl.userInteractionEnabled = YES;  
    labelGovUrl.tag = K_NNGOV_WEBSITE_LABEL_URL;  
  
    UITapGestureRecognizer *tapGesture =  
    [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openURL:)] autorelease];  
    [labelGovUrl addGestureRecognizer:tapGesture];  
    [self.view addSubview:labelGovUrl];  
    [labelGovUrl release];  
  
  
-(void)openURL:(UITapGestureRecognizer *)gesture{  
    NSInteger tag = gesture.view.tag;  
    NSString *url = nil;  
    if (tag == K_NNWEIBO_LABEL_URL) {  
        url = @"http://t.qq.com/yourgame/";  
    }  
    if(tag == K_NNGOV_WEBSITE_LABEL_URL){  
        url = @"http://www.zjnn.cn/";  
    }  
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];  
}  
相關文章
相關標籤/搜索