2009-11-09 14 views
5

मैं 90 डिग्री घुमाए गए दृश्य में कुछ पाठ खींचना चाहता हूं। मैं आईफोन विकास के लिए काफी नया हूं, और वेब के चारों ओर पोकिंग कई अलग-अलग समाधान बताता है। मैंने कुछ कोशिश की है और आम तौर पर मेरे पाठ को फिसलने के साथ समाप्त होता है।आईफोन: घुमावदार पाठ ड्रा?

यहां क्या हो रहा है? मैं पूर्वाह्न काफी छोटी जगह (एक टेबल व्यू सेल) में चित्रित करता हूं, लेकिन ऐसा करने के लिए "सही" तरीका होना चाहिए ... सही?


संपादित करें: यहाँ उदाहरण के एक जोड़े हैं। मैं बाईं ओर ब्लैक बार के साथ "" टेक्स्ट प्रदर्शित करने का प्रयास कर रहा हूं।

  1. पहला प्रयास RJShearman on the Apple Discussions

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSelectFont (context, "Helvetica-Bold", 16.0, kCGEncodingMacRoman); 
    CGContextSetTextDrawingMode (context, kCGTextFill); 
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0); 
    CGContextSetTextMatrix (context, CGAffineTransformRotate(CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f), M_PI/2)); 
    CGContextShowTextAtPoint (context, 21.0, 55.0, [_cell.number cStringUsingEncoding:NSUTF8StringEncoding], [_cell.number length]); 
    CGContextRestoreGState(context); 
    

    Attempt one. The one and part of the two are clipped out. http://dev.deeptechinc.com/sidney/share/iphonerotation/attempt1.png

  2. दूसरा प्रयास, से zgombosi on iPhone Dev SDK से। समान परिणाम (फ़ॉन्ट यहां थोड़ा छोटा था, इसलिए कम क्लिपिंग है)।

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGPoint point = CGPointMake(6.0, 50.0); 
    CGContextSaveGState(context); 
    CGContextTranslateCTM(context, point.x, point.y); 
    CGAffineTransform textTransform = CGAffineTransformMakeRotation(-1.57); 
    CGContextConcatCTM(context, textTransform); 
    CGContextTranslateCTM(context, -point.x, -point.y); 
    [[UIColor redColor] set]; 
    [_cell.number drawAtPoint:point withFont:[UIFont fontWithName:@"Helvetica-Bold" size:14.0]]; 
    CGContextRestoreGState(context); 
    

    Attempt two. There is almost identical clipping http://dev.deeptechinc.com/sidney/share/iphonerotation/attempt2.png

+0

इस बारे में जाने के कुछ तरीके हैं। आपने वास्तव में क्या प्रयास किया है और यह वास्तव में कैसे काम नहीं किया है (यह कैसे/कहाँ फिसल गया?) उपयोगी जानकारी का जवाब देना आसान बना देगा। –

+0

विवरण प्रदान किया गया। – s4y

उत्तर

7

यह पता चला है कि मेरा टेबल सेल हमेशा पंक्ति ऊंचाई के बावजूद 44px उच्च प्रारंभ किया गया था, इसलिए मेरी सभी ड्राइंग सेल के शीर्ष से 44px को बंद कर रही थी।

बड़ा कोशिकाओं आकर्षित करने के लिए यह निर्धारित करने के लिए सामग्री को देखने के autoresizingMask

cellContentView.autoresizingMask = UIViewAutoresizingFlexibleHeight; 

या

cellContentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

... और drawRect साथ सही आकार के साथ कहा जाता है जरूरी हो गया था। एक तरह से, यह समझ में आता है, क्योंकि UITableViewCell का initWithStyle:reuseIdentifier: सेल के आकार का कोई उल्लेख नहीं करता है, और केवल तालिका दृश्य वास्तव में जानता है कि प्रत्येक पंक्ति कितनी बड़ी होगी, अपने आकार के आधार पर और tableView:heightForRowAtIndexPath: पर इसके प्रतिनिधि की प्रतिक्रिया ।

मैं Quartz 2D Programming Guide पढ़ा जब तक ड्राइंग मॉडल और कार्यों समझ बनाने के लिए शुरू कर दिया, और मेरे घुमाया पाठ आकर्षित करने के लिए कोड सरल और स्पष्ट हो गया:

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSaveGState(context); 
CGContextRotateCTM(context, -(M_PI/2)); 
[_cell.number drawAtPoint:CGPointMake(-57.0, 5.5) withFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0]]; 
CGContextRestoreGState(context); 

सुझावों के लिए धन्यवाद, ऐसा लगता है कि मैं कर रहा हूँ सब तैयार।

1

आप UITableViewDelegate विधि heightForRowAtIndexPath अधिकार के बारे में पता है?

यहां एक simple tutorial on various graphics level methods. है जो आपको पता है कि आपका टेक्स्ट कितना बड़ा है, आप अपनी तालिका को आकार के आकार को उचित रूप से आकार देने में सक्षम होना चाहिए।

इसके अलावा, मैं यह सुनिश्चित करने के लिए जांच करता हूं कि किसी भी बदलाव के बाद सीमाएं वास्तव में आपकी अपेक्षाओं को पूरा करती हैं। (या तो इसे सत्यापित करने के लिए डीबगर या लॉग स्टेटमेंट का उपयोग करें)।

+0

मुझे 'tableView: heightForRowAtIndexPath: 'के बारे में पता है, पंक्ति स्वयं ठीक है। पाठ ट्रांसफॉर्म के दौरान फिसल रहा है और सेल के बीच में कट ऑफ समाप्त होता है। – s4y

+0

क्या आप हमें उस विशिष्ट कोड को दिखा सकते हैं जिसका उपयोग आप ट्रांसफॉर्म करने के लिए करते हैं? –

2

यहां एक टिप है। मुझे लगता है कि आप DrawRect में यह चित्र कर रहे हैं। रेक्ट कितना बड़ा है और यदि आप क्लिपिंग प्राप्त करते हैं, तो आप drawRect के चारों ओर एक फ्रेम क्यों नहीं खींचते हैं।

एक विकल्प है कि आप अपना टेक्स्ट UILabel में रखें, और फिर उस 90 डिग्री घुमाएं जब आप सेल कोorFowRowAtIndexPath में बनाते हैं।

+2

महबौडज़ का सुझाव शायद कम से कम प्रतिरोध का मार्ग होगा। आप UILabel 90deg को इसके साथ घुमा सकते हैं: [लेबल सेट ट्रांसफॉर्म: CGAffineTransformMakeRotation (DegreesToRadians (-90.0f))]; लेबल चौड़ाई के आधार पर आपको बस अपनी सेल ऊंचाई की गणना करनी होगी। -मैट –

0

मुझे पता चला कि मुझे अपनी फ़ाइल के शीर्ष पर निम्न जोड़ने की आवश्यकता है, मुझे मैट का दृष्टिकोण पसंद आया। बहुत आसान।

#define degreesToRadian(x) (M_PI * (x)/180.0) 

महबौडज़ का सुझाव शायद कम से कम प्रतिरोध का मार्ग होगा। आप UILabel 90deg को इसके साथ घुमा सकते हैं: [लेबल सेट ट्रांसफॉर्म: CGAffineTransformMakeRotation (DegreesToRadians (-90.0f))]; लेबल चौड़ाई के आधार पर आपको बस अपनी सेल ऊंचाई की गणना करनी होगी। -मैट - मैट लांग नवंबर 10 पर 0:09

3

का उपयोग करें: -

label.transform = CGAffineTransformMakeRotation(- 90.0f * M_PI/180.0f); 

जहां लेबल UILabel की वस्तु है।

+0

धन्यवाद, @ रद्द! असल में, मैं 'उइलाबेल' का उपयोग नहीं कर रहा था - मैं एक कस्टम टेबल व्यू सेल में चित्रित कर रहा था। – s4y

1

क्या @Sidnicious ने कहा, और मैं क्या बाहर ढेर अतिप्रवाह के माध्यम से एकत्र, मैं एक के उपयोग का उदाहरण देना चाहता हूँ के लिए - मेरी कोड संलग्न पूरी तरह से छोड़ दिया स्क्रीन ओर करने के लिए एक शासक आकर्षित करने के लिए, संख्या घुमाया साथ:

ruler screenshot

RulerView : UIView 


    // simple testing for iPhones (check for device descriptions to get all iPhones + iPads) 
    - (float)getPPI 
    { 
     switch ((int)[UIScreen mainScreen].bounds.size.height) { 
      case 568: // iPhone 5* 
      case 667: // iPhone 6 
       return 163.0; 
       break; 

      case 736: // iPhone 6+ 
       return 154.0; 
       break; 

      default: 
       return -1.0; 
       break; 
     } 
    } 

    - (void)drawRect:(CGRect)rect 
    { 
     [[UIColor blackColor] setFill]; 


     float ppi = [self getPPI]; 

     if (ppi == -1.0) // unable to draw, maybe an ipad. 
      return; 

     float linesDist = ppi/25.4; // ppi/mm per inch (regular size iPad would be 132.0, iPhone6+ 154.0) 

     float linesWidthShort = 15.0; 
     float linesWidthMid = 20.0; 
     float linesWidthLong = 25.0; 

     for (float i = 0, c = 0; i <= self.bounds.size.height; i = i + linesDist, c = c +1.0) 
     { 
      bool isMid = (int)c % 5 == 0; 
      bool isLong = (int)c % 10 == 0; 

      float linesWidth = isLong ? linesWidthLong : isMid ? linesWidthMid : linesWidthShort; 
      UIRectFillUsingBlendMode((CGRect){0, i, linesWidth, .5} , kCGBlendModeNormal); 



      /* FONT: Numbers without rotation (yes, is short) 
      if (isLong && i > 0 && (int)c % 10 == 0) 
       [[NSString stringWithFormat:@"%d", (int)(c/10)] drawAtPoint:(CGPoint){linesWidthLong +2, i -5} withAttributes:@{ 
                                   NSFontAttributeName: [UIFont systemFontOfSize:9], 
                                   NSBaselineOffsetAttributeName: [NSNumber numberWithFloat:1.0] 
                                   }]; 
      */ 


      // FONT: Numbers with rotation (yes, requires more effort) 
      if (isLong && i > 0 && (int)c % 10 == 0) 
      { 
       NSString *str = [NSString stringWithFormat:@"%d", (int)(c/10)]; 
       NSDictionary *attrs = @{ 
             NSFontAttributeName: [UIFont systemFontOfSize:9], 
             NSBaselineOffsetAttributeName: [NSNumber numberWithFloat:0.0] 
             }; 
       CGSize textSize = [str sizeWithAttributes:attrs]; 


       CGContextRef context = UIGraphicsGetCurrentContext(); 
       CGContextSaveGState(context); 

       CGContextRotateCTM(context, +(M_PI/2)); 
       [str drawAtPoint:(CGPoint){i - (textSize.width/2), -(linesWidthLong + textSize.height +2)} withAttributes:attrs]; 

       CGContextRestoreGState(context); 
      } 

     } 
    } 
संबंधित मुद्दे