2014-01-16 4 views
8

내 Xamarin.iOS 앱에서 패딩을 사용하여 UILabel을 만들려고합니다. 기본 목표 - C 응용 프로그램에서 가장 인기있는 솔루션은 drawTextInRect을 무시한다 :이 보인다Xamarin.iOS에서 패딩이있는 UILabel?

- (void)drawTextInRect:(CGRect)rect { 
    UIEdgeInsets insets = {0, 5, 0, 5}; 
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; 
} 

으로 간단하게, 내가 꽤 C 번호로 번역하는 방법을 알아낼 수 없습니다.

internal class PaddedLabel : UILabel 
{ 
    public UIEdgeInsets Insets { get; set; } 

    public override void DrawText(RectangleF rect) 
    { 
     var padded = new RectangleF(rect.X + Insets.Left, rect.Y, rext.Width + Insets.Left + Insets.Right, rect.Height); 

     base.DrawText(padded); 
    } 
} 

이 레이블의 텍스트를 이동하는 것 않지만, 라벨의 크기를 조정하지 않습니다 여기에 최선 자상입니다.

나는 주된 이슈가 UIEdgeInsetsInsetRect의 Xamarin에 해당하지 않는다고 생각한다.

제안 사항?

답변

16

ObjC 함수 UIEdgeInsetsInsetRect의 C# 1 당량 UIEdgeInsets라는 InsetRect의 인스턴스 방법이며 (가능성 문제이다) RectangleF 계산하여 동일 아니다. 내가 UIView의에서 파생되는 모든 IOS UI 요소를 감싸는 일반적인 패딩 UIView의 클래스를 생성 한

public override void DrawText(RectangleF rect) 
{ 
    base.DrawText (Insets.InsetRect (rect)); 
} 
+0

고마워요! 이것은 Objective-C 버전의 올바른 번역 인 것으로 보입니다. 예상대로 텍스트를 이동하지만 텍스트는 오른쪽으로 잘립니다. 즉, 인서트를 수용하기 위해 'UILabel'의 크기가 조정되지 않습니다. 하지만 그것은 별개의 문제 일 수 있습니다. –

+0

그 코드는 드로잉을 변경하기 만하므로'UILabel'은 인세 트를 인식하지 못합니다 (또한 동작을 변경하지 않습니다). – poupou

+0

그건 의미가 있습니다.라벨 너비를 결정할 때 인세 트를 고려해야 할 부분에 대한 제안은 무엇입니까? 가능한 경우 동적으로 크기를 조정하고 싶습니다. –

1

:

은 당신이 할 수있는 사용합니다.

기본적으로 원하는 UIView를 다른보기로 중첩하고 모든 채우기 작업을 처리합니다.

사용 :

public class PaddedUIView<T>: UIView where T : UIView, new() 
{ 
    private float _padding; 
    private T _nestedView; 

    public PaddedUIView() 
    { 
     Initialize(); 
    } 

    public PaddedUIView(RectangleF bounds) 
     : base(bounds) 
    { 
     Initialize(); 
    } 

    void Initialize() 
    { 
     if(_nestedView == null) 
     { 
      _nestedView = new T(); 
      this.AddSubview(_nestedView); 
     } 
     _nestedView.Frame = new RectangleF(_padding,_padding,Frame.Width - 2 * _padding, Frame.Height - 2 * _padding); 
    } 

    public T NestedView 
    { 
     get { return _nestedView; } 
    } 

    public float Padding 
    { 
     get { return _padding; } 
     set { if(value != _padding) { _padding = value; Initialize(); }} 
    } 

    public override RectangleF Frame 
    { 
     get { return base.Frame; } 
     set { base.Frame = value; Initialize(); } 
    } 
} 
+0

public override RectangleF Frame,이 메서드는 해결하지 않습니다. – Signcodeindie

0

오히려 UILabel의의 하위 클래스에서 DrawText에()를 오버라이드 (override)보다, 그것은 고유 콘텐츠 크기의 오버라이드 (override) : 여기

var myPaddedView = new PaddedUIView<UILabel>(); 
myPaddedView.Frame = TheActualFrame; 
myPaddedView.Padding = 15f 
myPaddedView.NestedView.Text = "Hello padded world"; // all the label Properties are available without side effects 

는 클래스입니다. 자동 레이아웃은 패딩을 고려합니다.

public class PaddedLabel : UILabel 
{ 
    private readonly float _top; 
    private readonly float _left; 
    private readonly float _right; 
    private readonly float _bottom; 

    public PaddedLabel(float top, float left, float right, float bottom) 
    { 
     _top = top; 
     _left = left; 
     _right = right; 
     _bottom = bottom; 
    } 

    public override CGSize IntrinsicContentSize => new CGSize(
     base.IntrinsicContentSize.Width + _left + _right, 
     base.IntrinsicContentSize.Height + _top + _bottom 
    ); 
} 
0

이 작업을 수행하는 올바른 방법은 원래의 대답이 주어진 이후 약간 변경 한 것, 그리고 새로운 올바른 구문을 알아 내기 위해 잠시 나를 데려 예를 들어 여기 UILabel의 내 파생 클래스입니다. 여기에 걸쳐 다른 사람을위한 것입니다.

public override void DrawText(CGRect rect) 
{ 
    rect.X = 5; 
    rect.Width = rect.Width - 10; // or whatever padding settings you want 
    base.DrawText(AlignmentRectInsets.InsetRect(rect)); 
} 
0

당신은 bothDrawTextTextRectForBounds를 재정의해야합니다. TextRectForBounds을 재정의하지 않으면 텍스트가 잘립니다. 실제로이 방법을 재정 의하여 패딩이 차지하는 공간을 보완하고 iOS에게 더 큰 직사각형에 텍스트를 그리도록 요청합니다.

public partial class LabelWithBorder 
    : UILabel 
{ 
    private UIEdgeInsets EdgeInsets = new UIEdgeInsets(5, 5, 5, 5); 
    private UIEdgeInsets InverseEdgeInsets = new UIEdgeInsets(-5, -5, -5, -5); 

    public LabelWithBorder(IntPtr handle) : base(handle) 
    { 
    } 

    public override CoreGraphics.CGRect TextRectForBounds(CoreGraphics.CGRect bounds, nint numberOfLines) 
    { 
     var textRect = base.TextRectForBounds(EdgeInsets.InsetRect(bounds), numberOfLines); 
     return InverseEdgeInsets.InsetRect(textRect); 
    } 

    public override void DrawText(CoreGraphics.CGRect rect) 
    { 
     base.DrawText(EdgeInsets.InsetRect(rect)); 
    } 
}