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
4

1 回答 1

0

UITextField如果您希望系统剪切/复制/粘贴菜单(或其中的某些子集)没有来自or的全套文本可编辑性功能UITextView,您需要提供自己的UIMenuController. 这样做需要:

  • 受菜单影响的控件可以成为第一响应者
  • 所述控件(或在其上方的响应者链中的某些合理的东西)实现了copy:您想要支持的编辑方法(例如)
  • 您执行一些事件处理以在适当的时间呈现菜单控制器(例如手势识别器操作)

通常情况下,NSHipster 上有一个很好的教程,其中包含 Swift 和 ObjC 示例代码。感谢@mattt,虽然我想说这条线需要更正:

如果您想知道为什么,哦,为什么,这不仅仅是内置UILabel的,嗯……加入俱乐部。

“加入俱乐部”应替换为“提交错误”。它甚至有点押韵!

于 2016-01-22T22:47:46.230 回答