Click here to Skip to main content
12,699,214 members (24,771 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked

AutoLayout and UILabel Don't Get Along

, 12 Dec 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This handy tip is an easy workaround for a shortcoming in iOS 6 and 7.


AutoLayout constraints are a huge improvement for page layout, but it doesn't always act the way it should. Take the UILabel for example. When constrained to another control, and the number of lines is set to 0, the text should be forced to wrap due to the UILabel's constraints. Unfortunately what you get looks like this. The constraint on the UILabel is ignored.


Luckily there is a simple workaround, shown here in C# (for Xamarin.iOS).

using MonoTouch.UIKit;

namespace RedCell.UI.Controls
    /// <summary>
    /// A workaround to allow wrapping with Auto Layout.
    /// </summary>
    internal class CustomUILabel : UILabel
        #region Overrides of UIView
        /// <summary>
        /// Lays out subviews.
        /// </summary>
        public override void LayoutSubviews ()
            this.PreferredMaxLayoutWidth = this.Superview.Bounds.Size.Width;

Subclassing UIView and overriding LayoutSubviews, we can set PreferredMaxLayoutWidth to the width of the control's parent, resulting the expected result:

Simply use the new class in place of UILabel.


  • December 9, 2013: Initial publication.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Yvan Rodrigues
Systems Engineer Clearpath Robotics
Canada Canada
Yvan Rodrigues has 25 years of experience in information systems and software development for the industry. He is Senior Systems Developer at Clearpath Robotics

He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).

Yvan draws on experience as owner of Red Cell Innovation Inc., Mabel's Labels Inc. as Manager of Systems and Development, and the University of Waterloo as Information Systems Manager.

Yvan supports open-source software. He is a committer for SharpKit (C# to Javascript cross-compiler) and WebIssues (Issue/Ticket Management System), TinyMCE (JavaScript editor), and contributes to MySQL, Ghostscript, iTextSharp, Bacula, FreeBSD, and Xamarin.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 12 Dec 2013
Article Copyright 2013 by Yvan Rodrigues
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid