2012年5月17日木曜日

CTFrameGetLineOrigins

CoreTextで描画した文字のframeの高さを取るだけで死ぬほど苦労した。CTFrameGetLineOriginsってのを使えばいいのはわかったけど参考事例が少なすぎる上に皆バラバラの記述で何が何やらわからん。やっとの思いでたどり着いて宣言プロパティにoriginの入ったcgpointを入れた。これでリンクテキストのしたにボタンを置けた。たったこれだけのことで何時間費やしたのか。またひとつ賢くなった。

#import "LinkedText.h"

@implementation LinkedText

@synthesize origin;

- (id)initWithFrame:(CGRect)frame string:(NSString *)string
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
  NSMutableDictionary *attrDic=[NSMutableDictionary dictionaryWithCapacity:0];
  CTFontRef ctFont=CTFontCreateWithName((__bridge CFStringRef)@"Helvetica",24, NULL);
  [attrDic setObject:(__bridge id)ctFont forKey:(NSString *) kCTFontAttributeName];
  [attrDic setObject:(__bridge id)[UIColor blueColor].CGColor forKey:(NSString *)kCTForegroundColorAttributeName];
  [attrDic setObject:[NSNumber numberWithInt:(int)kCTUnderlineStyleSingle] forKey:(NSString *)kCTUnderlineStyleAttributeName];
  NSMutableAttributedString *linkedText=[[NSMutableAttributedString alloc]initWithString:string attributes:attrDic];
  CGMutablePathRef path=CGPathCreateMutable();
  CTFramesetterRef framesetter= CTFramesetterCreateWithAttributedString((__bridge CFMutableAttributedStringRef)linkedText);;
  CGPathAddRect(path, NULL, self.bounds);
  ctFrame=CTFramesetterCreateFrame(framesetter,CFRangeMake(0, [linkedText length]),path,NULL);
  CFArrayRef lines=CTFrameGetLines(ctFrame);
  CFIndex lastitem=CFArrayGetCount(lines)-1;
  CTFrameGetLineOrigins(ctFrame, CFRangeMake(lastitem,1), &origin);
  CGPathRelease(path);
  self.backgroundColor=[UIColor clearColor];
    }
    return self;
}

できた。originを呼び元のrootのviewcontrollerから参照してボタンを置いて、あとはUIWebViewでリンク先を開くだけ。これはacessTokenを取る時と同じ感じでいけるはず。本当か。

夜は職場の人とサシで御飯食べて更にそこから角力@玉造でコクしょうゆらーめん。美味いが、若干甘いかな。満腹。

0 件のコメント:

コメントを投稿