Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8766051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:27:04+00:00 2026-06-13T16:27:04+00:00

I’m creating app where UILabel object will move over the screen. My question is

  • 0

I’m creating app where UILabel object will move over the screen. My question is how I can restrict the label to go over the specific border. For example if the label have a message, I want have the whole message visible, not just the first part of it.
This is the code:

#define kHeight     320.0
#define kWidth          400.0
#define kTransitionDuration 1.50
#define kTopPlacement       80.0

- (void)myMover {


for (UIView *view in self.view.subviews) {
      if( [view isKindOfClass:[UILabel class]]){
    [UIView animateWithDuration:4.0 animations:^{  

            //set the point from where the move will start
            [self setRandomLocationForLabel:view];

        }];
    }

}

}


- (void) setRandomLocationForView:(UIView *)view
 {
  [view sizeToFit];      
   CGRect messageViewBounds = CGRectMake(round((self.view.bounds.size.width - kWidth) / 2.0),
                          200, kWidth, kHeight);

CGRect newFrame = CGRectMake( 0, 300, 100 , 20 );
while (view.frame.size.width > kWidth) {

    newFrame.size.width /= 2;
    newFrame.size.height /= 2;

}


view.frame = newFrame;

CGFloat x = (CGFloat) (arc4random() % (int) messageViewBounds.size.width + view.frame.size.width/2);
CGFloat y = (CGFloat) (arc4random() % (int) messageViewBounds.size.height + view.frame.size.height/2);


view.center = CGPointMake (x,y);

}

Thank you for any advice!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:27:06+00:00Added an answer on June 13, 2026 at 4:27 pm

    Since you have the view you know its size. By knowing the width you know how far you need to position the center of the view from left or right (half the width). By knowing the height you know how from top or bottom (half the height).

    Now you can calculate the rectangle that contains only the valid center points for that view by taking the full view and create an inset rectangle with half the width of your view subtracted on both left and right and half the height of your view subtracted on both top and bottom.

    CGSize viewSize = view.bounds.size; // The view you are positioning
    CGRect rectOfValidCenters = CGRectInset(self.view.bounds, // The view you are placing it in
                                            viewSize.width/2.0, // subtract the width/2 from left and right  
                                            viewSize.height/2.0); // subtract the height/2 form top and bottom
    CGFloat randomX = // generate random value from 0.0 to 1.0
    CGFloat randomY = // generate random value from 0.0 to 1.0
    
    // Random valid center point is: 
    //  ( minX + randomX * width , minY + randomY * height)
    //
    // if x and y are zero then the view's center will be in the upper left
    // of the rect of valid centers (making its upper left corner be in the
    // top left of the view it's placed in).
    // if x and y are one then the view's center will be in the lower right
    // of the rect of valid centers (making its lower right corner be in the
    // lower right of the view it's placed in).
    CGPoint randomValidPoint = CGPointMake(CGRectGetMinX(rectOfValidCenters) + randomX * CGRectGetWidth(rectOfValidCenters),
                                           CGRectGetMinY(rectOfValidCenters) + randomY * CGRectGetHeight(rectOfValidCenters));
    view.center = randomValidPoint;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.