如何保存并发送 NSMutableAttributedString (使用 coreText 获得)到服务器?
Hi all,
我正在尝试将 UITextView
的选定文本加粗。为此,我使用 coreText 方法。在此,我可以使用 NSMutableAtrribulatedString
将选定的文本加粗并显示在上下文中。
-(void)drawRect:(CGRect)rect
{
NSLog(@"return str.....%@",[PISTrialViewController returnString]);
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
initWithString:[PISTrialViewController returnString]];
NSLog(@"attrrrr.....%@",string);
tempRange=[PISTrialViewController returnRange];
// make a few words bold
CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), 14.0, NULL);
CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 14.0, NULL);
NSLog(@"location.....%d",tempRange.location);
NSLog(@"length.....%d",tempRange.length);
[string addAttribute:(id)kCTFontAttributeName
value:(id)helveticaBold
range:NSMakeRange(tempRange.location,tempRange.length)];
// layout master
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(
(CFAttributedStringRef)string);
// left column form
CGMutablePathRef leftColumnPath = CGPathCreateMutable();
CGPathAddRect(leftColumnPath, NULL,
CGRectMake(0, 0,
self.bounds.size.width,
self.bounds.size.height));
// left column frame
CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,
CFRangeMake(0, 0),
leftColumnPath, NULL);
context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0);
// draw
CTFrameDraw(leftFrame, context);
// cleanup
CFRelease(leftFrame);
CGPathRelease(leftColumnPath);
CFRelease(framesetter);
CFRelease(helvetica);
CFRelease(helveticaBold);
[string release];
}
我从 UITextView 中选择了一些文本,并将该文本发送到 UIView 的子类,我在其中编写了此方法(drawRect)。在 uiview 中绘制的上下文将所选文本显示为粗体。但现在我无法进一步选择文本。如何才能我这样做吗?
如何在 sqlite 数据库中保存/检索 NSMutableAtrribulatedString
?
我如何将其发送到服务器?(以哪种格式?)
Hi all,
I am trying to bold the selected text of UITextView
.For that i am using coreText approach.In this i am able to bold the selected text and display on context using NSMutableAtrributedString
.
-(void)drawRect:(CGRect)rect
{
NSLog(@"return str.....%@",[PISTrialViewController returnString]);
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
initWithString:[PISTrialViewController returnString]];
NSLog(@"attrrrr.....%@",string);
tempRange=[PISTrialViewController returnRange];
// make a few words bold
CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), 14.0, NULL);
CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 14.0, NULL);
NSLog(@"location.....%d",tempRange.location);
NSLog(@"length.....%d",tempRange.length);
[string addAttribute:(id)kCTFontAttributeName
value:(id)helveticaBold
range:NSMakeRange(tempRange.location,tempRange.length)];
// layout master
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(
(CFAttributedStringRef)string);
// left column form
CGMutablePathRef leftColumnPath = CGPathCreateMutable();
CGPathAddRect(leftColumnPath, NULL,
CGRectMake(0, 0,
self.bounds.size.width,
self.bounds.size.height));
// left column frame
CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,
CFRangeMake(0, 0),
leftColumnPath, NULL);
context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0);
// draw
CTFrameDraw(leftFrame, context);
// cleanup
CFRelease(leftFrame);
CGPathRelease(leftColumnPath);
CFRelease(framesetter);
CFRelease(helvetica);
CFRelease(helveticaBold);
[string release];
}
I have selected some text from UITextView and send that text to subclass of UIView where i have written this method(drawRect).The context drawn in uiview displays the selected text as bold.But now i am unable to select the text further.How can i do that?
How to save/retrieve NSMutableAtrributedString
in/from sqlite database?
How can i send it on server?(in which format?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSMutableAtrribulatedString 实现了 NSCoding 协议,因此您可以使用 NSKeyedArchiver 来序列化它们。
NSMutableAtrributedString implements the NSCoding protocol, so you can use NSKeyedArchiver to serialize them.