2013-02-08 40 views
0

如下所示,我通過擬合圖像中的文本來獲得「泡泡」中的文本。它工作正常,除非文字不可複製。我需要「複製」選項,這樣在顯示上的文本用戶選項卡(或泡沫圖像「)‘警察’的選項,就像在用戶選項卡插入內置的SMS消息的泡沫。如何將「複製」選項添加到文本?

#import "SpeechBubbleView.h" 

static UIFont* font = nil; 
static UIImage* lefthandImage = nil; 
static UIImage* righthandImage = nil; 

const CGFloat VertPadding = 4;  // additional padding around the edges 
const CGFloat HorzPadding = 4; 

const CGFloat TextLeftMargin = 17; // insets for the text 
const CGFloat TextRightMargin = 15; 
const CGFloat TextTopMargin = 10; 
const CGFloat TextBottomMargin = 11; 

const CGFloat MinBubbleWidth = 50; // minimum width of the bubble 
const CGFloat MinBubbleHeight = 40; // minimum height of the bubble 

const CGFloat WrapWidth = 200;  // maximum width of text in the bubble 

@implementation SpeechBubbleView 

+ (void)initialize 
{ 
    if (self == [SpeechBubbleView class]) 
    { 
     font = /*[*/ [UIFont systemFontOfSize:[UIFont systemFontSize]] /*retain]*/; 

     lefthandImage = /*[*/ [[UIImage imageNamed:/*@"BubbleLefthand"*/@"lefttest4"] 
      stretchableImageWithLeftCapWidth:20 topCapHeight:19] /*retain]*/; 

     righthandImage = /*[*/[[UIImage imageNamed:/*@"BubbleRighthand"*/@"righttest4"] 
      stretchableImageWithLeftCapWidth:20 topCapHeight:19] /*retain]*/; 
    } 
} 

+ (CGSize)sizeForText:(NSString*)text 
{ 
    CGSize textSize = [text sizeWithFont:font 
     constrainedToSize:CGSizeMake(WrapWidth, 9999) 
     lineBreakMode:UILineBreakModeWordWrap]; 

    CGSize bubbleSize; 
    bubbleSize.width = textSize.width + TextLeftMargin + TextRightMargin; 
    bubbleSize.height = textSize.height + TextTopMargin + TextBottomMargin; 

    if (bubbleSize.width < MinBubbleWidth) 
     bubbleSize.width = MinBubbleWidth; 

    if (bubbleSize.height < MinBubbleHeight) 
     bubbleSize.height = MinBubbleHeight; 

    bubbleSize.width += HorzPadding*2; 
    bubbleSize.height += VertPadding*2; 

    return bubbleSize; 
} 

- (void)drawRect:(CGRect)rect 
{ 
    [self.backgroundColor setFill]; 
    UIRectFill(rect); 

    CGRect bubbleRect = CGRectInset(self.bounds, VertPadding, HorzPadding); 

    CGRect textRect; 
    textRect.origin.y = bubbleRect.origin.y + TextTopMargin; 
    textRect.size.width = bubbleRect.size.width - TextLeftMargin - TextRightMargin; 
    textRect.size.height = bubbleRect.size.height - TextTopMargin - TextBottomMargin; 

    if (bubbleType == BubbleTypeLefthand) 
    { 
     [lefthandImage drawInRect:bubbleRect]; 
     textRect.origin.x = bubbleRect.origin.x + TextLeftMargin; 
    } 
    else 
    { 
     [righthandImage drawInRect:bubbleRect]; 
     textRect.origin.x = bubbleRect.origin.x + TextRightMargin; 
    } 

    [[UIColor blackColor] set]; 
    [text drawInRect:textRect withFont:font lineBreakMode:UILineBreakModeWordWrap]; 
} 

- (void)setText:(NSString*)newText bubbleType:(BubbleType)newBubbleType 
{ 
    //[text release]; 
    /** 
    handle local 
    **/ 

    // text = [newText copy]; 

    text = [newText copy]; 

    bubbleType = newBubbleType; 
    [self setNeedsDisplay]; 
} 

- (void)dealloc 
{ 
    //[text release]; 
    //[super dealloc]; 
} 

@end 
+1

爲什麼不使用'UITextView'或'UITextField'作爲子視圖呢?當然,你可以使用'UIMenuController',但我不確定你想要手動將所有這一切破解。 – 2013-02-08 16:52:46

+0

謝謝,...如果我使用UITextView,我可以隱藏矩形框,以便只有文本出現在泡泡內(不在textview框內)? – user836026 2013-02-08 17:52:28

+0

@userXXX'UITextView'默認情況下有明確的背景,我不明白你的問題是什麼。 – 2013-02-08 17:53:25

回答

0

如果你想讓系統剪切/複製/粘貼菜單(或其某個子集),而從UITextFieldUITextView全套的文字可編輯功能,您需要出示自己的UIMenuController這樣做需要:

  • 控制受菜單影響可成爲第一響應者
  • 表示控制(或者在其上面的響應者鏈中是合理的)實現編輯方法(例如, copy:)要支持
  • 你執行一些事件處理在適當的時候出示菜單控制器(例如手勢識別動作)

由於通常情況下,有a good tutorial on NSHipster既斯威夫特和ObjC樣本碼。榮譽對@mattt,雖然我會說這行需要修正:

如果你想知道爲什麼,哦,爲什麼,這不僅僅是內置UILabel,嗯...加入這個俱樂部。

「加入俱樂部」應改爲「file a bug」。它甚至幾乎押韻!

相關問題