0

以下に示すように、テキストを画像内に合わせて「バブル」にテキストを入れます。テキストがコピーできないことを除いて、正常に機能します。テキスト(またはバブル画像)のユーザータブで「cop」オプションが表示されるように、「コピー」オプションが必要です。これは、組み込みの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またはのテキスト編集機能をすべて使用せずに、システムの切り取り/コピー/貼り付けメニュー (またはそのサブセット)UITextViewが必要な場合は、独自の を提示する必要がありますUIMenuController。これを行うには、次のものが必要です。

  • メニューの影響を受けるコントロールは、最初の応答者になることができます
  • 上記のコントロール(またはその上のレスポンダーチェーンで適切なもの)は、copy:サポートしたい編集メソッド(例)を実装します
  • 適切なタイミングでメニュー コントローラを表示するために何らかのイベント処理を実行します (たとえば、ジェスチャ レコグナイザ アクション)。

よくあることですが、NSHipsterには、Swift と ObjC の両方のサンプル コードを含む優れたチュートリアルがあります。@mattt に敬意を表しますが、この行には修正が必要だと思います。

なぜ、ああ、なぜ、これは に組み込まれているだけではないのか疑問に思っている場合はUILabel、まあ… クラブに参加してください。

「クラブに参加する」は「バグを報告する」に置き換えてください。それはほとんど韻を踏むことさえあります!

于 2016-01-22T22:47:46.230 に答える