나는 this과 this을 읽었지만 문제를 해결하지 못하는 것 같습니다."도난당한"UILabel의 레이어를 다른 CALayer의 하위 레이어에 추가하는 것이 안전합니까?
- (NSAttributedString *) attributedStringForText: (NSString *) text {
NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:text];
UIFont * bigF = [UIFont fontWithName:@"OpenSans-Extrabold" size:20.0f] ;
UIFont * smaF = [UIFont fontWithName:@"OpenSans-Extrabold" size:12.0f] ;
[attrStr addAttribute:NSFontAttributeName value:bigF range:(NSRange){0, 3}] ;
[attrStr addAttribute:NSFontAttributeName value:smaF range:(NSRange){3, 6}] ;
[attrStr addAttribute:NSFontAttributeName value:bigF range:(NSRange){6, [text length]-6}] ;
[attrStr addAttribute:NSBackgroundColorAttributeName value:[UIColor brownColor] range:(NSRange){3, 6}] ;
return attrStr ;
}
- (CALayer *) stealLayerFromUILabelForText: (NSString *) text inRect: (CGRect) bounds {
UILabel * label = [[UILabel alloc] initWithFrame:bounds] ;
label.textColor = [UIColor whiteColor] ;
label.font = [UIFont fontWithName:@"OpenSans-Extrabold" size:20.0f] ;
label.attributedText = [self attributedStringForText:text] ;
[label sizeToFit] ;
[label.layer display] ; // Yup!
return label.layer ;
}
및
- (void) setupLayer: (CALayer *) tileLayer
text: (NSString *) text
index: (NSUInteger) index {
CGRect (^center_rect)(CGRect, CGRect) = ^CGRect (CGRect r, CGRect into) {
CGRect centered = into ;
centered.origin.x += (into.size.width - r.size.width)/2.0f ;
centered.origin.y += (into.size.height - r.size.height)/2.0f ;
centered.size = r.size ;
return centered ;
} ;
CALayer * layer = [self stealLayerFromUILabelForText:text inRect:tileLayer.bounds] ;
CGRect frame = center_rect(layer.bounds, tileLayer.bounds) ;
[tileLayer addSublayer:layer] ;
layer.frame = frame ;
}
을이 작동 :
... NSCoding
과 친구들의 접선을 이동하는 것, 그래서
하지만 나는 어떻게 든 UILabel을 학대합니다 & CALayer. 한편 UIView
, UILabel
모두는 실제CALayer
계층 구조의 상단에 바로 얇은 터치 인식 베니어 있습니다. OTOH 나는 어쨌든 여기에 명시된 가정조차 거의 사용하지 않고있다.
누구나 이것이 더 이상 길을 깨는 이유를 알 수 없으며 그렇지 않은 경우 더 나은 증거가 될 수 있습니다.
[CALayer display]의 첫 번째 줄에는 "이 방법을 직접 호출하지 마십시오."라는 것이 있습니다. 즉, 애플이 미래에 무언가를 바꾸면 깨질 수 있습니다. 아마도 가능성은 없지만 확실히 가능할 수 있습니다. – nsdebug
'iOS Core Animation : Advanced Techniques Nick Lockwood'의 사례가 있습니다. http://books.google.co.uk/books?id=QfhdAAAAQBAJ&pg=PT117&lpg=PT117&dq=we+should+really+derive+these+ + UILabel + 설정 + 너무 +하지만 + % 27s + 복잡, + so + for + now + 우리는 % 27ll + just + hard-code + 그들을 & 소스 = bl & ots = zw23tDc5QeUYABIBtAVMg2eEQCc & hl = en & sa = X & ei = 55dSU_jNNMWjO-ntgNgL & = 0CC8Q6AEwAA # v = onepage & q = 우리 % 20 실제로 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % 20 % % 20them & f = false ... – verec
iOS 7의 스냅 샷 API를 사용할 수 있습니다 – Felix