NSAttributedString 다색 투각 문자열 그리 기

3601 단어 ios
오리지널 블 로그, 전재 출처 를 밝 혀 주세요.
http://blog.csdn.net/hello_hwc?viewmode=contents
NSAttributeString 은 말 그대로 다양한 속성 을 가 진 문 자 를 입 을 수 있 고 문자열 의 단일 문자 속성 (글꼴, 색상, 밑줄 등) 속성 을 관리 하 는 방식 을 제공 합 니 다.
NSObject 로부터 계승 되 기 때문에 UIKit 의 특성 을 가지 지 않 습 니 다.
우선 목표 효과 도 를 살 펴 보 겠 습 니 다.
그리고
첫 번 째 인 터 페 이 스 를 설계 합 니 다.
UIView 의 하위 클래스 DrawStringView 만 들 기
그리고 이 종류의 인 터 페 이 스 는 두 개 입 니 다.
1 표시 되 는 String 내용
2 초기 화 방법
이렇게 DrawStringView. h 의 전체 코드 는 다음 과 같 습 니 다.
#import <UIKit/UIKit.h>

@interface DrawStringView : UIView
@property (strong,nonatomic)NSString * contentString;
-(id)initWithContent:(NSString *)content Frame:(CGRect)frame;
@end

두 번 째 단계 완성 실현
구체 적 인 과정 은 코드 주석 참조
이렇게 하면 전체 코드 는 다음 과 같다.
</pre><pre name="code" class="objc">//
//  DrawStringView.m
//  ProjectForCSDN
//
//  Created by huangwenchen on 14/12/25.
//  Copyright (c) 2014  huangwenchen. All rights reserved.
//

#import "DrawStringView.h"

@implementation DrawStringView

#pragma mark - property
//    contentString,       
-(void)setContentString:(NSString *)contentString{
    _contentString = contentString;
    [self setNeedsDisplay];//    ,  drawRect
}
#pragma mark - redraw
-(void)drawRect:(CGRect)rect{
    NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = NSTextAlignmentCenter;//  
    style.lineBreakMode = NSLineBreakByWordWrapping;//       
    UIFont * font = [UIFont fontWithName:@"Helvetica" size:30];
    NSMutableAttributedString * attributeStirng = [[NSMutableAttributedString alloc]initWithString:self.contentString];
    NSDictionary *  firstPartAttributes = @{ NSFontAttributeName:font,//   
                                            NSParagraphStyleAttributeName:style,//    
                                            NSForegroundColorAttributeName:[UIColor whiteColor],//   
                                            NSStrokeColorAttributeName:[UIColor blueColor],//   
                                            NSStrokeWidthAttributeName:@(3)//    
                                            };
    NSDictionary * secondPartAttributes = @{ NSFontAttributeName:font,
                                             NSParagraphStyleAttributeName:style,
                                             NSForegroundColorAttributeName:[UIColor whiteColor],
                                             NSStrokeColorAttributeName:[UIColor redColor],
                                             NSStrokeWidthAttributeName:@(3)
                                             };
    NSUInteger halfLocation = (int)attributeStirng.length/2;
    [attributeStirng addAttributes:firstPartAttributes range:NSMakeRange(0,halfLocation)];//      
    [attributeStirng addAttributes:secondPartAttributes range:NSMakeRange(halfLocation,attributeStirng.length - halfLocation)];//      
    [attributeStirng drawInRect:self.bounds];
}
#pragma mark - init method
-(id)initWithContent:(NSString *)content Frame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.contentString = content;
        self.opaque = NO;//   
    }
    return self;
}
@end
물론 NSAttributeString 은 더 많은 속성 이 있 습 니 다. 이상 은 자주 사용 하 는 몇 가지 이 고 더 많은 속성 은 XCode 의 문 서 를 참조 합 니 다. 상세 합 니 다.
3. 이런 종 류 를 사용한다.
UIView * viewWithString = [[DrawStringView alloc] initWithContent:@"This is test string" Frame:CGRectMake(100, 100, 200, 200)];
[self.view addSubview:viewWithString];

좋은 웹페이지 즐겨찾기