如何保存并发送 NSMutableAttributedString (使用 coreText 获得)到服务器?

发布于 2024-10-12 20:05:50 字数 2142 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呢古 2024-10-19 20:05:50

NSMutableAtrribulatedString 实现了 NSCoding 协议,因此您可以使用 NSKeyedArchiver 来序列化它们。

NSMutableAtrributedString implements the NSCoding protocol, so you can use NSKeyedArchiver to serialize them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文