2011-03-12 15 views

cevap

12

Evet, CoreText bunu yapmanın en iyi yoludur. Bunu yapabilmek için kod yazmanız biraz uzun. Doğru yönde size işaret edebilecek bazı kodlar, "Clipping a CGRect to a CGPath." numaralı makalede yer alıyor. Bunu yapmak için işlem hala kafa karıştırıcı ise, ping ve son olarak blog yazısı yazdım.

+0

Yardımınıza ihtiyacım var. Kodu blog yayınınızda tam olarak anlamıyorum. Bu konuda bir makale yazabilir misiniz? Teşekkür ederim! – nonamelive

+0

Bunun hakkında unutmadım; Onu bir araya getirmek biraz zaman aldı. –

+7

Biraz kaba ama umarım doğru yönde ilerlemenizi sağlar. http://robnapier.net/blog/wrapping-text-around-shape-with-coretext-540 –

1

Adım 1:

UIView

miras bir nesne oluşturma Adım 2: Aşırı yükleme - (void), drawRect: (CGRect) rect yöntemi

Adım 3: Bu yöntem

/* Define some defaults */ 
float padding = 10.0f; 

/* Get the graphics context for drawing */ 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 

/* Core Text Coordinate System is OSX style */ 
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity); 
CGContextTranslateCTM(ctx, 0, self.bounds.size.height); 
CGContextScaleCTM(ctx, 1.0, -1.0); 
CGRect textRect = CGRectMake(padding, padding, self.frame.size.width - padding*2, self.frame.size.height - padding*2); 

/* Create a path to draw in and add our text path */ 
CGMutablePathRef pathToRenderIn = CGPathCreateMutable(); 
CGPathAddRect(pathToRenderIn, NULL, textRect); 

/* Add a image path to clip around, region where you want to place image */ 
CGRect clipRect = CGRectMake(padding, self.frame.size.height-50, 50, 40); 
CGPathAddRect(pathToRenderIn, NULL, clipRect); 

/* Build up an attributed string with the correct font */ 
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.Text]; 

//setFont 
CTFontRef font = CTFontCreateWithName((CFStringRef) [UIFont systemFontOfSize:10].fontName, [UIFont systemFontOfSize:10].lineHeight, NULL); 
CFAttributedStringSetAttribute((CFMutableAttributedStringRef) attrString, CFRangeMake(0, attrString.length), kCTFontAttributeName,font); 

//set text color 
CGColorRef _white=[UIColor whiteColor].CGColor; 
CFAttributedStringSetAttribute((CFMutableAttributedStringRef)(attrString), CFRangeMake(0, attrString.length),kCTForegroundColorAttributeName, _white); 

/* Get a framesetter to draw the actual text */ 
CTFramesetterRef fs = CTFramesetterCreateWithAttributedString((CFAttributedStringRef) attrString); 
CTFrameRef frame = CTFramesetterCreateFrame(fs, CFRangeMake(0, attrString.length), pathToRenderIn, NULL); 

/* Draw the text */ 
CTFrameDraw(frame, ctx); 

/* Release the stuff we used */ 
CFRelease(frame); 
CFRelease(pathToRenderIn); 
CFRelease(fs); 
aşağıdaki kodu ekleyin

Adım 4: Aşağıdaki gibi kullanın;

TextLayoutView *TextWrappingImage=[[TextLayoutView alloc] init...your own constructor...]; 

TextWrappingImage.backgroundColor=[UIColor clearColor]; 

[cell addSubview:TextWrappingImage]; //Add as subview where you want 
0

Benzer sorun. Standart işaretleme etiketlerini kullanarak HTML ile metin içeren metinlerimi oluşturarak çözdüm, projeme html ve pngs ekledim ve bir UIWebView kullanarak görüntüledim. :-)

+0

+1. En zarif çözüm olmayabilir, ancak bu yöntemi kullandım ve bunu işlemek için bir kod satırı ve sadece bir div öğesindeki bazı basit CSS .. 'width: 95px; yüksekliği: 95px; kenar boşluğu: 5px 5px 5px 0px; kenarlık: 1px katı #eee; yüzer: sol; arka plan: URL (% @) merkezi merkezi; Arka plan-tekrar: no-tekrar; background-size: include; 'Bence çok daha küçük ve daha kolay uygulanıyor. Teşekkürler – thefoyer