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];
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift의 패스트 패스Objective-C를 대체하기 위해 만들어졌지만 Xcode는 Objective-C 런타임 라이브러리를 사용하기 때문에 Swift와 함께 C, C++ 및 Objective-C를 컴파일할 수 있습니다. Xcode는 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.